<?php
	header("Content-type: text/html;charset=utf8");
	require_once("../db/dbclass.php");
	require_once("../model/userinfo.php");
	require_once("../utils/common.php");
	require_once("../utils/config.php");
	$myDb=&DbController::getInstance();
	$tweet_id=$_POST['tweet_id'];
	$user_id=$_POST['user_id'];
	$time=$_POST['time'];
	$result="failure";
	$param="";
	switch ($time) {
		case '0'://历史
			$param="SELECT * FROM yj_tweets WHERE (tweets_id < $tweet_id) and ((user_id = $user_id) or (user_id in (SELECT user1_id FROM yj_friend WHERE user2_id = $user_id ))) LIMIT 20";
			//$param="SELECT * FROM (SELECT * FROM yj_tweets WHERE tweets_id < $tweet_id ) AS t1 INNER JOIN(SELECT user1_id FROM yj_friend WHERE user2_id = $user_id ) AS t2 ON t1.user_id=t2.user1_id OR t1.user_id=$user_id LIMIT 20";
			break;
		case '1'://最新
			$param="SELECT * FROM yj_tweets WHERE (tweets_id > $tweet_id) and ((user_id = $user_id) or (user_id in (SELECT user1_id FROM yj_friend WHERE user2_id = $user_id )))";
			// $param="SELECT * FROM (SELECT * FROM yj_tweets WHERE tweets_id > $tweet_id ) AS t1 INNER JOIN(SELECT user1_id FROM yj_friend WHERE user2_id = $user_id ) AS t2 ON t1.user_id=t2.user1_id";
			break;
		default:
			break;
	}
	if($param==""){
		$arr=array(
			"result"=>"failure",
			"tweets"=>""
		);
	}
	else{
		$result_query=$myDb->exec_query($param);
		if($result_query==TRUE){
			$row_num = mysqli_num_rows($result_query);
			$result="success";
			$array_all=array($row_num);
			$i=0;
			while($row=mysqli_fetch_array($result_query,MYSQL_ASSOC)){
				$img_array = explode(';',$row['tweets_img']);
				$img_newurl=""; 
				for($index=0;$index<count($img_array)-1;$index++) 
				{ 	
					if($index== count($img_array)-2){
						$img_newurl=$img_newurl.URL.substr($img_array[$index],2);
					}
					else{
						$img_newurl=$img_newurl.URL.substr($img_array[$index],2).";";
					}
				} 
				$upvote_status="0";
				$tweets_id=$row['tweets_id'];
				$param="SELECT user_id from yj_upvote_record WHERE tweets_id='$tweets_id' AND user_id='$user_id'";
				if(mysqli_num_rows($myDb->exec_query($param)) >= 1){
					$upvote_status="1";
				}
				$array_i=array(
					"friend_id"=>$row['user_id'],
					"tweets_id"=>$row['tweets_id'],
					"comment_num"=>$row['comment_num'],
					"upvote_num"=>$row['upvote_num'],
					"upvote_status"=>$upvote_status,
					"tweets_content"=>$row['tweets_content'],
					"tweets_img"=>$img_newurl,
					"tweets_time"=>$row['tweets_time']
					);
					
				$array_all[$i++]=$array_i;
			}
		}
		$arr=array(
			"result"=>$result,
			"tweets"=>$array_all
		);
	}
	echo json_encode($arr);
?>